package com.itboye.ihomebank.util;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.custom.datepicker.wheelview.OnWheelScrollListener;
import com.itboye.ihomebank.custom.datepicker.wheelview.WheelView;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.ArrayWheelAdapter;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.NumericWheelAdapter;

import java.util.Calendar;

/**
 * 时间（发送钥匙用到  时间   年月日时分秒）
 */
public class ChooseTimePopwindow extends PopupWindow {
	private TextView open_camera, pick_image, camera_cancel_tv;
	private View cameraMenuView;
	private int mYear = 1996;
	private int mMonth = 0;
	private int mDay = 1;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView time;
	private WheelView min;
	private WheelView sec;
	Activity ac;
	onFinishedLisenter onfinishlistener;
	boolean isStart = true;

	@SuppressWarnings("deprecation")
	public ChooseTimePopwindow(Activity context,
			final onFinishedLisenter onfinishlistener, final boolean isStart) {
		super(context);
		this.ac = context;
		this.isStart = isStart;
		this.onfinishlistener = onfinishlistener;
		Calendar c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR) + 1;
		int curYear = mYear;
		int curMonth = c.get(Calendar.MONTH);
		int curDate;
		if (isStart){
			curDate = c.get(Calendar.DATE);
		}else{//结束日期比开始日期少一天
			curDate = c.get(Calendar.DATE)-1;
		}
		int curHour=c.get(Calendar.HOUR_OF_DAY);
		int curMin=c.get(Calendar.MINUTE);
		View view = LayoutInflater.from(context).inflate(
				R.layout.popup_choosetime, null);
		year = (WheelView) view.findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
				context, norYear - 1, norYear+3);
		numericWheelAdapter1.setLabel("年");
		year.setViewAdapter(numericWheelAdapter1);
		year.setCurrentItem(norYear);
		year.setCyclic(false);// �Ƿ��ѭ������
		year.addScrollingListener(scrollListener);

		month = (WheelView) view.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
				context, 1, 12, "%02d");
		numericWheelAdapter2.setLabel("月");
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);

		day = (WheelView) view.findViewById(R.id.day);
		initDay(curYear, curMonth, context);
		day.setCyclic(true);

		time = (WheelView) view.findViewById(R.id.time);
		String[] times = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
				"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
				"21", "22", "23", "24" };
		ArrayWheelAdapter<String> arrayWheelAdapter = new ArrayWheelAdapter<String>(
				context, times);
		time.setViewAdapter(arrayWheelAdapter);
		time.setCyclic(true);
		time.addScrollingListener(scrollListener);

		min = (WheelView) view.findViewById(R.id.min);
		NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(
				context, 1, 59, "%02d");
		numericWheelAdapter3.setLabel("分");
		min.setViewAdapter(numericWheelAdapter3);
		min.setCyclic(true);
		min.addScrollingListener(scrollListener);

		sec = (WheelView) view.findViewById(R.id.sec);
		NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(
				context, 1, 59, "%02d");
		numericWheelAdapter4.setLabel("秒");
		sec.setViewAdapter(numericWheelAdapter4);
		sec.setCyclic(true);
		sec.addScrollingListener(scrollListener);

		year.setVisibleItems(7);// ������ʾ����
		month.setVisibleItems(7);
		day.setVisibleItems(7);
		time.setVisibleItems(7);
		min.setVisibleItems(7);
		sec.setVisibleItems(7);

		year.setCurrentItem(curYear - 1950);
		month.setCurrentItem(curMonth);
		day.setCurrentItem(curDate - 1);
		time.setCurrentItem(curHour-1);
		min.setCurrentItem(curMin-1);
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		this.setContentView(view);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		// this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0xb000000);
		this.setBackgroundDrawable(dw);
		this.cameraMenuView = view.findViewById(R.id.choosetime);
		cameraMenuView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// int height = cameraMenuView.findViewById(R.id.choosetime)
				// .getTop();
				// int y = (int) event.getY();
				// if (event.getAction() == MotionEvent.ACTION_UP) {
				// if (y < height) {
				// dismiss();
				// }
				// }

				return true;
			}
		});
		TextView cancel = (TextView) view.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});

		TextView ok = (TextView) view.findViewById(R.id.ok);
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isStart)
					onfinishlistener.getBeginTime(
							(year.getCurrentItem() + 2017) + "",
							(1 + month.getCurrentItem()) + "",
							(1 + day.getCurrentItem()) + "",
							(1 + time.getCurrentItem()) + "",
							(1 + min.getCurrentItem()) + "");
				else {
					onfinishlistener.getEndTime((year.getCurrentItem() + 2017)
							+ "", (1 + month.getCurrentItem()) + "",
							(1 + day.getCurrentItem()) + "",
							(1 + time.getCurrentItem()) + "",
							(1 + min.getCurrentItem()) + "");
				}
				dismiss();
			}
		});

	}

	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {
			// if (isStart){
			//
			// }
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {

		}
	};

	/**
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

	private void initDay(int arg1, int arg2, Context c) {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(c, 1,
				getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		day.setViewAdapter(numericWheelAdapter);
	}

	public interface onFinishedLisenter {
		public void getBeginTime(String year, String month, String day,
				String hour, String minite);

		public void getEndTime(String year, String month, String day,
				String hour, String minite);
	}

}
